
// console.log
const isArray = function (obj: any): boolean {
  return Object.prototype.toString.call(obj) === '[object Array]'
}

class Logger {
  static typeColor(type: string): string {
    let color = ''
    switch (type) {
      case 'primary':
        color = '#2d8cf0'
        break
      case 'success':
        color = '#19be6b'
        break
      case 'info':
        color = '#909399'
        break
      case 'warn':
        color = '#ff9900'
        break
      case 'error':
        color = '#f03f14'
        break
      default:
        color = '#35495E'
        break
    }
    return color
  }
  static print(type: string, text: any, back?: boolean) {
    if (typeof text === 'object') {
      // 如果是對象則調用打印對象方式
      isArray(text) ? console.table(text) : console.dir(text)
      return
    }
    if (back) {
      // 如果是打印帶背景圖的
      console.log(
        `%c ${text} `,
        `background:${Logger.typeColor(type)}; padding: 2px; border-radius: 4px; color: #fff;`,
      )
    } else {
      console.log(
        `%c ${text} `,
        `border: 1px solid ${Logger.typeColor(type)};
          padding: 2px; border-radius: 4px;
          color: ${Logger.typeColor(type)};`,
      )
    }
  }
  static printBackGround(type: string, text: any) {
    this.print(type, text, true)
  }
  static pretty(type: string, title: string, text: any) {
    if (typeof text === 'object') {
      // console.group('Console Group', title)
      console.log(
        `%c ${title}`,
        `background:${Logger.typeColor(type)};border:1px solid ${Logger.typeColor(type)};
          padding: 1px; border-radius: 4px 0 0 4px; color: #fff;`,
      )
      isArray(text) ? console.table(text) : console.dir(text)
      // console.groupEnd()
      return
    }
    console.log(
      `%c ${title} %c ${text} %c`,
      `background:${Logger.typeColor(type)};border:1px solid ${Logger.typeColor(type)};
        padding: 1px; border-radius: 4px 0 0 4px; color: #fff;`,
      `border:1px solid ${Logger.typeColor(type)};
        padding: 1px; border-radius: 0 4px 4px 0; color: ${Logger.typeColor(type)};`,
      'background:transparent',
    )
  }
  static prettyPrimary(title: string, ...text: any[]) {
    text.forEach((t) => this.pretty('primary', title, t))
  }
  static prettySuccess(title: string, ...text: any[]) {
    text.forEach((t) => this.pretty('success', title, t))
  }
  static prettyInfo(title: string, ...text: any[]) {
    text.forEach((t) => this.pretty('info', title, t))
  }
  static prettyWarn(title: string, ...text: any[]) {
    text.forEach((t) => this.pretty('warn', title, t))
  }
  static prettyError(title: string, ...text: any[]) {
    text.forEach((t) => this.pretty('error', title, t))
  }

  static primary(...text: any[]) {
    text.forEach((t) => this.print('primary', t))
  }
  static success(...text: any[]) {
    text.forEach((t) => this.print('success', t))
  }
  static info(...text: any[]) {
    text.forEach((t) => this.print('info', t))
  }
  static warn(...text: any[]) {
    text.forEach((t) => this.print('warn', t))
  }
  static error(...text: any[]) {
    text.forEach((t) => this.print('error', t))
  }
}
export default Logger
